package com.cgly.medical.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @program: CGLY
 * @description:
 * @author: chen di
 * @create: 2022-08-23 15:07
 */
@Component
public class MailClient {

    private static final Logger logger = LoggerFactory.getLogger(MailClient.class);

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    public void sendMail(String to, String subject, String content) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            mailSender.send(helper.getMimeMessage());
        } catch (MessagingException e) {
            logger.error("发送邮件失败:" + e.getMessage());
        }
    }


    /**
     * 发送html邮件
     *
     * @param
     * @param subject
     * @param content
     */
    public void sendHtmlMail(String to,String subject, String content, String filePathExcel, String filePathPdf) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from); //配置在yml文件中的发件人邮箱
            helper.setTo(to);  //配置在yml文件中的收件人邮箱
//            String[] cc = {ccone,cctwo}; //将两个抄送邮箱放进String[]中作为一个参数传值
//            helper.setCc(cc); //设置抄送人邮箱
//            helper.setBcc(bcc);  //设置隐秘抄送人邮箱
            helper.setSubject(subject);  //设置标题
            helper.setText(content, true); //设置邮件内容，并开启H5

            // 判断是否带有Excel附件
            if (filePathExcel != null) {
                FileSystemResource file = new FileSystemResource(new File(filePathExcel));
                String fileName = filePathExcel.substring(filePathExcel.lastIndexOf(File.separator));
                helper.addAttachment(fileName, file);
            }
            // 判断是否带有Pdf附件
            if (filePathPdf != null) {
                FileSystemResource file = new FileSystemResource(new File(filePathPdf));
                String fileName = filePathPdf.substring(filePathPdf.lastIndexOf(File.separator));
                helper.addAttachment(fileName, file);
            }
            mailSender.send(message);
            logger.info("定时报表邮件发送成功");
        } catch (MessagingException e) {
            logger.error("发送定时报表邮件时发生异常！", e);
        }
    }


}